package com.control;

import com.model.*;
import com.service.FinanceService;
import com.service.RecordService;
import com.service.impl.JdFinanceServiceImpl;
import com.service.impl.RecordServiceImpl;
import com.service.impl.YuebaoFinanceServiceImpl;
import com.model.*;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@RestController
public class AccountingController {

    private static final List<AccountingRecord> records = new ArrayList<>();
    private static final List<Payinfo> payinfos = new ArrayList<>();
    private static final RecordService recordService = new RecordServiceImpl();

    @PostConstruct
    public void init(){
        //初始化白条数据
        Payinfo baitiao = new Payinfo();
        baitiao.setPayType(PayType.baitiao);
        baitiao.setBillingDate(10);
        baitiao.setDueDate(20);
        payinfos.add(baitiao);

        //初始化花呗数据
        Payinfo huabei = new Payinfo();
        huabei.setPayType(PayType.huabei);
        huabei.setBillingDate(20);
        huabei.setDueDate(10);
        payinfos.add(huabei);

        //初始化信用卡数据
        CreditCard creditCard = new CreditCard();
        creditCard.setPayType(PayType.creditCard);
        creditCard.setBillingDate(5);
        creditCard.setDueDate(25);
        creditCard.setName("招商银行信用卡");
        creditCard.setCardNumber("1111111111");
        payinfos.add(creditCard);
    }
    @RequestMapping(path = "/accounting")
    public Accounting accounting(Accounting accounting){

        int result = accounting.getCash() + accounting.getIncome() - accounting.getRent() - accounting.getCharges() - accounting.getEat()
                     - accounting.getTreat() - accounting.getKtv();
        accounting.setBalance(result);
        return accounting;
    }
    @RequestMapping(path = "/record")
    public String record(AccountingRecord record){
        if(record.getAmount() == 0){
            return "";
        }
        Date time = new Date();
        record.setTime(time);
        //调用方法
        recordService.record(record);
        return "记录成功";
    }
    @RequestMapping(path = "/search")
    public String search(@RequestParam("amount") int amount){
        List<AccountingRecord> filters = records.stream()
                                .filter(record -> record.getAmount() > amount)
                                .collect(Collectors.toList());
        StringBuilder sb = new StringBuilder();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        for(AccountingRecord filter : filters){
            sb.append("记录:" + " 发生时间:" + filter.getCreateTime() + " " );
            sb.append(" 金额:" + filter.getAmount() + " " );
            sb.append(" 记录时间:" + sdf.format(filter.getTime()) + " ");
            sb.append(" 科目:" + filter.getType() + " " );
            sb.append(" 类别:" + filter.getCategory() + "\n");
        }
        return sb.toString();
    }
    @RequestMapping(path = "/pay")
    public Payinfo pay(@RequestParam("amount") double amount, @RequestParam("payType") PayType payType,
                        @RequestParam("stagesCount") int stagesCount){
        //过滤
        List<Payinfo> payinfoList = payinfos.stream()
                .filter(payinfo1 -> payinfo1.getPayType().equals(payType))
                .collect(Collectors.toList());
        //根据paytype构建数据对象
        if(payType.equals(PayType.creditCard)){
            CreditCard creditCard = (CreditCard)payinfoList.get(0);
            //返回数据
            CreditCard result = new CreditCard();
            result.setBillingDate(creditCard.getBillingDate());
            result.setDueDate(creditCard.getDueDate());
            result.setPayType(creditCard.getPayType());
            result.setAmount(amount);
            result.setStagesCount(stagesCount);
            result.setName(creditCard.getName());
            result.setCardNumber(creditCard.getCardNumber());
            return result;
        }else{
            Payinfo payinfo = payinfoList.get(0);
            //返回数据//
            Payinfo result = new Payinfo();
            result.setBillingDate(payinfo.getBillingDate());
            result.setDueDate(payinfo.getDueDate());
            result.setPayType(payinfo.getPayType());
            result.setAmount(amount);
            result.setStagesCount(stagesCount);
            return result;
        }
    }
    @RequestMapping(path = "/finance")
    public FinanceInfo finance(@RequestParam("type") FinanceType financeType,
                               @RequestParam("amount") double amount,
                               @RequestParam("days") int days){
        FinanceService financeService = null;
        //根据不同的类型，实例化不同的服务实现者
        if(FinanceType.yuebao.equals(financeType)){
            financeService = new YuebaoFinanceServiceImpl();
        }else if(FinanceType.jd.equals(financeType)){
            financeService = new JdFinanceServiceImpl();
        }else{
            return null;
        }
        return financeService.invest(amount,days);
    }
    @RequestMapping(path = "/query")
    public List<AccountingRecord> query(){
        return recordService.query();
    }
}
